<?php
/**
 * 首页控制类
 * @author 大梦
 * @DateTime 2019/07/06
 */

namespace app\index\controller;

use think\Validate;
use app\common\model\Member as MemberModel;
use app\common\model\Weixin as WeixinModel;
use app\common\model\Activity as ActivityModel;
use app\common\model\ActivitySignUp as ActivitySignUpModel;

class Index extends Common
{

    /**
     * [index description]
     * @author Meng
     * @dateTime 2020-09-23
     * @return   [type]     [description]
     */
    public function index()
    {
        $params = input();
        $data = [
            'info' => '',
        ];
        $this->assign($data);
        return view();
    }

    /**
     * [index description]
     * @author Meng
     * @dateTime 2020-09-23
     * @return   [type]     [description]
     */
    public function detail()
    {
        $params = input();

        $aid = isset($params['aid']) ? $params['aid'] : '';
        $uid = empty(session('uid')) ? '' : session('uid');

        //跳转授权
        if (!session('uid')) {
            // $this->redirect('index/wechat/wechat_login', ['aid' => $aid]);
        }

        $where_activity = [
            ['id', 'eq', $aid],
            ['is_del', 'eq', 0],
        ];
        $activity_info = (new ActivityModel())->where($where_activity)->find() ?? '';

        $where_sign = [
            ['aid', 'eq', $aid],
            ['uid', 'eq', $uid],
            ['is_del', 'eq', 0],
        ];
        $sign_info = (new ActivitySignUpModel())->where($where_sign)->find() ?? '';

        $where_member = [
            ['id', 'eq', $uid],
            ['is_del', 'eq', 0],
        ];
        $member_info = (new MemberModel())->where($where_member)->find() ?? '';

        $data = [
            'sign_info' => '',
            'member_info' => $member_info,
            'activity_info' => $activity_info,
        ];

        if (isset($params['is_debug'])) {
            halt($data);
        }

        $this->assign($data);
        return view();
    }

    /**
     * [index description]
     * @author Meng
     * @dateTime 2020-09-23
     * @return   [type]     [description]
     */
    public function sign_up()
    {
        $params = input();


        //验证输入数据
        $rule = [];
        $rule0 = [
            'aid'   => 'require',
            'uid'   => 'require',
            'username'   => 'require',
            'id_card_no'   => 'require',
            'mobile'   => 'require|mobile',
            'email'   => 'require|email',
        ];
        $rule1 = [
            'graduated_school'   => 'require',
            'education'   => 'require',
            'graduation_year'   => 'require',
            'industry_type'   => 'require',
            'job_type'   => 'require',
            'expected_salary'   => 'require',
        ];
        $rule2 = [
            'school_name'   => 'require',
            'purpose'   => 'require',
        ];

        $error_msg = [
            'aid.require' => '请选择活动',
            'uid.require' => '请先授权登录',
            'username.require' => '请输入姓名',
            'id_card_no.require' => '请输入身份证号',
            'mobile.require' => '请输入手机号',
            'mobile.mobile' => '手机号格式不正确',
            'email.require' => '请输入邮箱',
            'email.email' => '邮箱格式不正确',
            'graduated_school.require' => '请输入毕业院校',
            'education.require' => '请选择学历',
            'graduation_year.require' => '请选择毕业年度',
            'industry_type.require' => '请选择意向行业',
            'job_type.require' => '请选择求职岗位',
            'expected_salary.require' => '请选择期望薪资',
            'school_name.require' => '请输入学校/公司',
            'purpose.require' => '请选择参会目的',
        ];

        if ($params['aid'] < 2) {
            $rule = array_merge($rule0, $rule1);
        } else {
            $rule = array_merge($rule0, $rule2);
        }

        $validate = Validate::make($rule, $error_msg);

        if (!$validate->check($params)) {
            return $this->api_error($validate->getError());
        }

        if (!validation_filter_id_card($params['id_card_no'])) {
            return $this->api_error('身份证号格式不正确');
        }

        $where = [
            ['is_del', 'eq', 0],
            ['aid', 'eq', $params['aid']],
        ];
        $where_or = [
            ['mobile', 'eq', $params['mobile']],
            ['username', 'eq', $params['username']],
            ['id_card_no', 'eq', $params['id_card_no']],
        ];

        // $sql = 'SELECT `id` FROM `td_activity_sign_up` WHERE (`mobile` = '15066886007' OR `username` LIKE '%15066886007%' OR `id_card_no` LIKE '%123%') AND `aid` =1 AND `is_del` = 1';

        $activitySignUpModel = (new ActivitySignUpModel());
        $sign_info = $activitySignUpModel
                ->where($where)
                ->where(function ($q) use($where_or) {
                    $q->whereOr($where_or);
                })
                ->find();

        if ($sign_info) {
            return $this->api_error('您当前已报名，无需重复报名');
        }

        $params['create_time'] = time();
        $sign_id = (new ActivitySignUpModel())->allowField(true)->insertGetId($params);

        $update_user_data = [
            'mobile' => $params['mobile'],
        ];

        $update_user = (new MemberModel())->where('id', $params['uid'])->update($update_user_data);

        $data = [
            'sign_id' => $sign_id,
        ];

        if (isset($params['is_debug'])) {
            halt($data);
        }

        return $this->api_success('报名成功', $data);
    }

    /**
     * [index description]
     * @author Meng
     * @dateTime 2020-09-23
     * @return   [type]     [description]
     */
    public function sign_result()
    {
        $params = input();

        $where_activity = [
            ['is_del', 'eq', 0],
            ['id', 'eq', $params['aid']],
        ];
        $activity_info = (new ActivityModel())->where($where_activity)->find();

        $where_sign = [
            ['is_del', 'eq', 0],
            ['id', 'eq', $params['sid']],
            ['aid', 'eq', $params['aid']],
        ];
        $sign_info = (new ActivitySignUpModel())->where($where_sign)->append(['sign_no'])->find();

        $data = [
            'activity_info' => $activity_info,
            'sign_info' => $sign_info,
        ];

        if (isset($params['is_debug'])) {
            halt($data);
        }
        
        $this->assign($data);
        return view();
    }

    public function get_mobile()
    {


        $appid = 'wxfb202f07c0c5a94b';
        $secret = '8c6ce6d8f55c619c1306f46e1850959d';
        $params = input();

        $js_code = $params['js_code'] ?? '';

        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxfb202f07c0c5a94b&secret=8c6ce6d8f55c619c1306f46e1850959d&js_code=' . $js_code . '&grant_type=authorization_code';
        $wx_data = get_by_curl($url);

        $sessionKey = $wx_data['session_key'];
        $iv = $params['iv'];
        $encryptedData = $params['encryptedData'];

        $data = '';
        $pc = new WeixinModel($appid, $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data );


        if ($errCode == 0) {
            return $this->api_success('获取成功', json_decode($data));
        } else {
            return $this->api_error('获取失败', $errCode);
        }

    }
}
